package com.xfly.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.bean.HdBean;
import com.xfly.app.bean.YhqBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HdActivity extends BaseMvpActivity
{

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    ImageView isby;
    TextView mj,mslist;
    TitleBarView title_bar;
    LinearLayout ms,yhq,mz;
    HdBean mHdBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_hd);
    }

    @Override
    public void initViews() {
        title_bar = findViewById(R.id.title_bar);
        mj = findViewById(R.id.mj);
        isby = findViewById(R.id.isby);
        ms = findViewById(R.id.ms);
        yhq = findViewById(R.id.yhq);
        mz = findViewById(R.id.mz);
        mslist = findViewById(R.id.mslist);

        title_bar.setText("活动设置");
    }

    @Override
    public void initEvents() {
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
        ms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(),MsActivity.class));
            }
        });
        mj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(),MjActivity.class));
            }
        });
        isby.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mHdBean.free_shipping.equals("1")){
                    setData("0");
                }else {
                    setData("1");
                }
            }
        });
        mz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(),MzActivity.class));
            }
        });
        yhq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(),YhqActivity.class));
            }
        });
        mslist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(),MsActivity.class));
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getHd();
    }

    private void getHd(){
        apiDataRepository.gethd(null, new ApiNetResponse<HdBean>(getContext()) {
            @Override
            public void onSuccess(HdBean data) {
                mHdBean = data;
                mj.setText("满"+data.full+"减"+data.full_reduce);
                if(data.free_shipping.equals("1")){
                    isby.setImageResource(R.drawable.on);
                }else {
                    isby.setImageResource(R.drawable.off);
                }
            }
        });

    }


    private void setData(String free_shipping){
        Map<String,String> map = new HashMap<>();
        map.put("free_shipping",free_shipping);
        map.put("postage",mHdBean.postage);
        map.put("full",mHdBean.full);
        map.put("full_reduce",mHdBean.full_reduce);
        apiDataRepository.setHd(map, new ApiNetResponse<Object>(getContext()) {
            @Override
            public void onSuccess(Object data) {
                showMsg("修改成功");
                getHd();
            }
        });
    }
}
